package com.market.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.Location;
import com.market.domain.Order;
import com.market.domain.OrderDetail;
import com.market.domain.Product;
import com.market.domain.Promotion;
import com.market.manager.PromotionManager;
import com.market.util.CollectionUtil;
import com.market.web.model.LocationModel;
import com.market.web.model.PromotionModel;


@Controller("promotionController")
@Scope("session")
public class PromotionController extends
		GenericController<PromotionManager, PromotionModel, Promotion> {
	@Autowired
	private LocationModel locationModel;
	
	@Autowired
	@Override
	protected void setModel(PromotionModel model) {
		super.model = model;
	}

	@Autowired
	@Override
	protected void setManager(PromotionManager manager) {
		super.manager = manager;
	}
	
	/**
	 * This method does nothing, just for setRemoveLocationFromCurrent work
	 * properly.
	 * 
	 * @param detail
	 */

	public void removeLocationFromCurrent() {

	}

	/**
	 * This method remove specific location from promotion.
	 * 
	 * @param location
	 */
	public void setRemoveLocationFromCurrent(Location location) {
		for (int i = 0; i < this.getModelCurrent().getLocations().size(); i++) {
			if (this.getModelCurrent().getLocations().get(i).equalsId(location)) {
				this.getModelCurrent().getLocations().remove(i);
			}
		}
	}
	
	public void addSelectedLocationToCurrent() {
		Promotion current = this.getModelCurrent();
		List<Location> selectedLocations = locationModel.getSelecteds();
		List<Location> locations = current.getLocations();
		boolean exist = false;
		for (Location location : selectedLocations) {
			exist = false;
			for (Location currentLocation : locations) {
				if (location.equalsId(currentLocation)) {
					exist = true;
					break;
				}
			}
			if (exist == false) {
				current.getLocations().add(location);
			}
		}
		CollectionUtil<Location> util = new CollectionUtil<Location>();
		util.clearSelection(selectedLocations);
	}
	
	
	public List<Promotion> fetchValidPromotion(Date date, Long locationId){
		return this.manager.fetchValidPromotions(date, locationId);
	}

}
